package com.visikard.request;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.visikard.application.APIURL;
import com.visikard.application.VKregisterProperties;
import com.visikard.item.ItemCouponsBuilderCreate;
import com.visikard.net.VkRequestMethod;
import com.visikard.net.VkRestClient;
import com.visikard.system.VKregisterConnects;
import com.visikard.utility.HttpsUtils;
import com.visikard.utility.Utils;

public class CouponRequest extends BaseRequest {

	public String getAllCoupons(long uid) throws Exception {
		String url = VKregisterConnects.getApiContextPathHttp()
				+ APIURL.VK_SPECIAL_CAMPAIGN_MERCHANT_GET_LIST_COUPON_VIREGISTER + uid;
		return Utils.getConnectValueFromURL(url);
	}

	public String uploadCBImage(String userId, ByteArrayOutputStream baosphoto) throws Exception {
		String result = null;
		HttpClient httpclient = HttpsUtils.getInstance().sslClient(new DefaultHttpClient());
		String httpurl = VKregisterConnects.getApiContextPathHttp() + APIURL.UPLOAD_COUPON_IMAGE;
		HttpPost httppost = new HttpPost(httpurl);
		MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
		reqEntity.addPart("fkUser", new StringBody(userId));
		reqEntity.addPart(new FormBodyPart("images", new ByteArrayBody(baosphoto.toByteArray(), "image/jpeg",
				"filename" + Calendar.getInstance().getTimeInMillis() + ".png")));
		httppost.setEntity(reqEntity);

		HttpResponse response = httpclient.execute(httppost);
		result = EntityUtils.toString(response.getEntity()).trim();
		return result;
	}

	public String createCoupon(long uid, String loginSession, String userId, ItemCouponsBuilderCreate item)
			throws Exception {
		List<NameValuePair> list = new ArrayList<NameValuePair>();

		list.add(new BasicNameValuePair("fkUser", String.valueOf(uid)));
		list.add(new BasicNameValuePair("sessionID", loginSession));
		list.add(new BasicNameValuePair("businessName", item.getCategoryName()));
		list.add(new BasicNameValuePair("businessAddress", item.getSubCategoryName()));
		list.add(new BasicNameValuePair("businesslatitude", ""));
		list.add(new BasicNameValuePair("businesslongitude", ""));
		list.add(new BasicNameValuePair("couponName", item.getCouponName()));
		list.add(new BasicNameValuePair("campaignDetailValue", "" + item.getCampaignDetailValue()));
		list.add(new BasicNameValuePair("conditions", item.getConditions()));
		list.add(new BasicNameValuePair("imageSpecial", item.getImageSpecial()));
		list.add(new BasicNameValuePair("imageText1", item.getImageText1()));
		list.add(new BasicNameValuePair("imageText2", item.getImageText2()));
		list.add(new BasicNameValuePair("sstartDate", item.getStartDate()));
		list.add(new BasicNameValuePair("sEndDate", item.getEndDate()));
		list.add(new BasicNameValuePair("termandcondition", item.getTermAndCondition()));
		list.add(new BasicNameValuePair("expireDate", item.getExpireDate()));
		list.add(new BasicNameValuePair("aboutbusiness", item.getAboutbusiness()));
		list.add(new BasicNameValuePair("listSpecialLocation", item.getListLocationsJSON()));
		list.add(new BasicNameValuePair("keyStore", VKregisterProperties.SERVER_KEYSTORE));

		return Utils.getPostConnectValueFromURL(APIURL.CREATE_COUPON, list);
	}

	public String updateCoupon(long uid, String loginSession, String userId, ItemCouponsBuilderCreate item)
			throws Exception {
		String couponImageName = "";
		try {
			String couponImageUrl = item.getImageSpecial();
			couponImageName = couponImageUrl.substring(item.getImageSpecial().lastIndexOf("/") + 1,
					couponImageUrl.length());
		}
		catch (Exception e) {

		}
		List<NameValuePair> list = new ArrayList<NameValuePair>();

		list.add(new BasicNameValuePair("fkUser", String.valueOf(uid)));
		list.add(new BasicNameValuePair("sessionID", loginSession));
		list.add(new BasicNameValuePair("id", "" + item.getCouponID()));
		list.add(new BasicNameValuePair("businessName", item.getCategoryName()));
		list.add(new BasicNameValuePair("businessAddress", item.getSubCategoryName()));
		list.add(new BasicNameValuePair("businesslatitude", ""));
		list.add(new BasicNameValuePair("businesslongitude", ""));
		list.add(new BasicNameValuePair("couponName", item.getCouponName()));
		list.add(new BasicNameValuePair("campaignDetailValue", "" + item.getCampaignDetailValue()));
		list.add(new BasicNameValuePair("conditions", item.getConditions()));
		list.add(new BasicNameValuePair("imageSpecial", couponImageName));
		list.add(new BasicNameValuePair("imageText1", item.getImageText1()));
		list.add(new BasicNameValuePair("imageText2", item.getImageText2()));
		list.add(new BasicNameValuePair("sstartDate", item.getStartDate()));
		list.add(new BasicNameValuePair("sEndDate", item.getEndDate()));
		list.add(new BasicNameValuePair("termandcondition", item.getTermAndCondition()));
		list.add(new BasicNameValuePair("expireDate", item.getExpireDate()));
		list.add(new BasicNameValuePair("aboutbusiness", item.getAboutbusiness()));
		list.add(new BasicNameValuePair("listSpecialLocation", item.getListLocationsJSON()));
		list.add(new BasicNameValuePair("keyStore", VKregisterProperties.SERVER_KEYSTORE));

		return Utils.getPostConnectValueFromURL(APIURL.UPDATE_COUPON, list);
	}

	public String getCouponDetail(long uid, int couponID) throws Exception {
		String url = VKregisterConnects.getApiContextPathHttp() + APIURL.GET_COUPON_DETAIL + uid + "/" + couponID;
		return Utils.getConnectValueFromURL(url);
	}

	public String deleteCouponsBuilder(long uid, Long fkUser, long id) throws Exception {
		List<NameValuePair> list = new ArrayList<NameValuePair>();

		list.add(new BasicNameValuePair("fkUser", String.valueOf(uid)));
		list.add(new BasicNameValuePair("id", "" + id));
		list.add(new BasicNameValuePair("keyStore", VKregisterProperties.SERVER_KEYSTORE));
		return Utils.getPostConnectValueFromURL(APIURL.DELETE_COUPON_BUILDER, list);
	}

	public String searchAddress(String address) throws Exception {
		String url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + address
				+ "&key=AIzaSyCv0i6cYssSKnMGRNmPnocy40m5iQp4nIg&sensor=false&types%20=%20geocode";
		return Utils.getConnectValueFromURL(url);
	}

	public String getRTUICoupons(String merchantIDFromRTUI) {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp() + APIURL.VK_RTUI_COUPON_GET);

		rc.addParam("merchantIDFromRTUI", merchantIDFromRTUI);
		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	public String requestRTUICoupon(long merchantId, String title, String contractNo, String message) {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp() + APIURL.VK_RTUI_COUPON_REQUEST);

		rc.addParam("merchantID", String.valueOf(merchantId));
		rc.addParam("title", title);
		rc.addParam("contractNo", contractNo);
		rc.addParam("message", message);

		rc.execute(VkRequestMethod.POST);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

}
